<?php
namespace app\base\controller;

use app\base\model\BanjiModel;
use app\base\model\StudentsModel;
use app\common\base\BasicRest;
use think\Config;
use think\Db;
use think\Request;

class Students extends  BasicRest
{
    public function __construct()
    {
        parent::__construct();
    }

    #获取学生信息
    public function search(Request $request){
        $data = $request->post();
        $page = $data['page'];
        unset($data['page']);
        foreach ($data as $key=>$value){
            if($value==''){
                unset($data[$key]);
            }
        }
        if(!empty($data['Name'])){
            $name = $data['Name'];
            $data['Name'] = ['like',"%$name%"];
        }
        $page_number = Config::get('page_number');
        //页数
        $count = Db::table('iStudent')->where($data)->where('IsDeleted',0)->count();
        $pages = $count/$page_number;
        $data1 = Db::table('iStudent')->alias('s')
            ->join('iBanji','iBanji.Id=s.Grade','LEFT')
            ->field('s.Id,Name,iBanji.Title as Grade,Number,Sex')
            ->where($data)
            ->where('s.IsDeleted',0)
            ->order('s.Id desc')
            ->page($page,$page_number)
            ->select();
        $data2 = Db::table('iStudent')->alias('s')
            ->join('iBanji','iBanji.Id=s.Class','LEFT')
            ->field('s.Id,Name,iBanji.Title as Class,Number,Sex')
            ->where($data)
            ->where('s.IsDeleted',0)
            ->order('s.Id desc')
            ->page($page,$page_number)
            ->select();
        $data = [];
        foreach ($data1 as $key=>$val){
                $data[] = array_merge($val,$data2[$key]);

        }
        if($data) return res_success($data,ceil($pages));
        return res_fail('未找到信息');
    }

    #获取年级
    public function getGrade(){
        $map = [
            'IsDeleted'=>0,
            'Kind'=>1
        ];
        $gradeData = BanjiModel::where($map)->select();
        if($gradeData) return res_success($gradeData);
        return res_fail('未找到信息');
    }

    #获取班级
    public function getClass(){
        $map = [
            'IsDeleted'=>0,
            'Kind'=>2
        ];
        $classData = BanjiModel::where($map)->select();
        if($classData) return res_success($classData);
        return res_fail('未找到信息');
    }

    #获取单个学生信息
    public function read($id){
        $data1 = Db::table('iStudent')->alias('s')
            ->join('iBanji','iBanji.Id=s.Grade','LEFT')
            ->field('s.Id,Name,iBanji.Title as Grade,Number,Sex,Birthday,Origin,Clan,Nation,Card,EnterTime,Address')
            ->where(['s.IsDeleted'=>0,'s.Id'=>$id])
            ->find();
        $data2 = Db::table('iStudent')->alias('s')
            ->join('iBanji','iBanji.Id=s.Class','LEFT')
            ->field('s.Id,Name,iBanji.Title as Class,Number,Sex,Birthday,Origin,Clan,Nation,Card,EnterTime,Address')
            ->where(['s.IsDeleted'=>0,'s.Id'=>$id])
            ->find();
        $data = array_merge($data1,$data2);
        if($data) return res_success($data);
        return res_fail('未找到信息');
    }

    #新增学生信息
    public function save(Request $request){
        $res = StudentsModel::create($request->post());
        if($res) return res_success();
        return res_fail('创建失败');
    }

    #修改学生信息
    public function update(Request $request,$id){
        $data = $request->put();
        unset($data['CreateId']);
        unset($data['CreateTime']);
        unset($data['UpdateId']);
        unset($data['UpdateTime']);
        $model = StudentsModel::find($id);
        $res = $model->save($data);
        if($res!== false) return res_success();
        return res_fail('修改失败');
    }

    #删除学生信息
    public function delete($id){
        $res = StudentsModel::where('Id',$id)->update(['IsDeleted'=>'1']);
        if($res) return res_success();
        return res_fail('删除失败');
    }

    #获取单个学生
    public function getOneData(Request $request){
        $id = $request->get('id');
        $data = StudentsModel::get($id);
        if($data) return res_success($data);
        return res_fail('未找到信息');
    }
}